package com.xiguaji.tiktok.task.aweme;

import cn.hutool.core.date.DateUtil;
import com.gugee.tiktok.common.model.AwemeScore;
import com.gugee.tiktok.common.model.dal.blogger.AwemeRepository;
import com.gugee.tiktok.common.model.dto.BloggerAwemeExtendDto;
import com.gugee.tiktok.common.model.dto.BloggerRecordAwemeExtendDto;
import com.gugee.tiktok.common.model.mysql.Aweme;
import com.xiguaji.boot.eventbus.EventBusTemplate;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import com.xiguaji.tiktok.task.aweme.event.ESRefreshAwemeEventListener;
import com.xiguaji.tiktok.task.aweme.event.SetAwemeScoreEventListener;
import com.xiguaji.tiktok.task.aweme.event.SaveBloggerAwemeEventListener;
import com.xiguaji.tiktok.task.monitor.TimeStatistics;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class AwemeService {
    @Autowired
    AwemeRepository awemeRepository;

    @Autowired
    @Lazy
    EventBusTemplate eventBusTemplate;

    public BloggerAwemeExtendDto getBloggerAwemeExtendDto(long uid) {
        Optional<BloggerAwemeExtendDto> bloggerAwemeExtendDto = awemeRepository
                .getBloggerAwemeExtend(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(90)));
        return bloggerAwemeExtendDto.isPresent() ? bloggerAwemeExtendDto.get() : null;
    }
    public BloggerRecordAwemeExtendDto getBloggerRecordAwemeExtendDto(long uid) {
        Optional<BloggerRecordAwemeExtendDto> bloggerRecordAwemeExtendDto = awemeRepository
                .getBloggerRecordAwemeExtend(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(90)));
        return bloggerRecordAwemeExtendDto.isPresent() ? bloggerRecordAwemeExtendDto.get() : null;
    }

    public BloggerRecordAwemeExtendDto getBloggerRecordAwemeExtendDto(long uid,int day) {
        Optional<BloggerRecordAwemeExtendDto> bloggerRecordAwemeExtendDto = awemeRepository
                .getBloggerRecordAwemeExtend(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(day)));
        return bloggerRecordAwemeExtendDto.isPresent() ? bloggerRecordAwemeExtendDto.get() : null;
    }

    public BloggerRecordAwemeExtendDto getBloggerRecordForZeroTimeAwemeExtendDto(long uid, int day) {
        Long timStamp = DateUtil.beginOfDay(new Date()).getTime();
        LocalDateTime  localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timStamp), ZoneId.systemDefault()).minusDays(day);
        Optional<BloggerRecordAwemeExtendDto> bloggerRecordAwemeExtendDto = awemeRepository
                .getBloggerRecordAwemeExtend(uid, localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond());
        return bloggerRecordAwemeExtendDto.isPresent() ? bloggerRecordAwemeExtendDto.get() : null;
    }


    public List<Aweme> getBloggerAwemes(long uid) {
        List<Aweme> bloggerAweme = awemeRepository.findAwemesByoUidAndoCreateTimeGreaterThan(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(90)));
        return bloggerAweme;
    }

    public List<Aweme> get24HourBloggerAwemes(long uid){
        List<Aweme> bloggerAweme = awemeRepository.findByUidAndBetween24Hour(uid);
        return bloggerAweme;
    }
    public List<Aweme> get30DayBloggerAwemes(long uid) {
        //调整为10天内视频 20200918 bby llf ,为评论而设置
        List<Aweme> bloggerAweme = awemeRepository.findAwemesByoUidAndoCreateTimeGreaterThan(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(10)));
        return bloggerAweme;
    }

    public TimeStatistics refreshBloggerAweme(long uid) {

        var saveBloggerAwemeEvent = new SaveBloggerAwemeEventListener.SaveBloggerAwemeEvent(Arrays.asList(uid));

//        try {
//            Thread t=  new Thread(()->{
                eventBusTemplate.post(saveBloggerAwemeEvent);
//               }
//            );
//            t.start();n
//            t.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }


        TimeStatistics timeStatistics=new TimeStatistics();
        timeStatistics.setAwemeSize(saveBloggerAwemeEvent.getAwemeSize());
        timeStatistics.setAwemeSpiderBloggerAwemes(saveBloggerAwemeEvent.getAwemeSpiderBloggerAwemes());
        timeStatistics.setSaveAwemeTimeStatistics(saveBloggerAwemeEvent.getSaveAwemeTimeStatistics());
        return timeStatistics;
    }

    public void refreshBloggerAwemeForES(long uid){
        val esRefreshAwemeEvent = new ESRefreshAwemeEventListener.ESRefreshAwemeEvent(uid);


//        try {
//            Thread t=  new Thread(()->{
                eventBusTemplate.post(esRefreshAwemeEvent);
//              }
//            );
//            t.start();
//            t.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    }

    public void refreshBloggerAwemeScore(AwemeScore tikTokScore){
        val setAwemeScoreEvent = new SetAwemeScoreEventListener.SetAwemeScoreEvent(tikTokScore);

//        try {
//            Thread t=  new Thread(()->{
                eventBusTemplate.post(setAwemeScoreEvent);
//            }
//            );
//            t.start();
//            t.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    public void deleteUserCollectionAweme(){
        long time = DateUtil.endOfDay(DateUtil.offsetMonth(new Date(),-1)).getTime()/1000;
        JdbcTemplateHelper.getJdbcTemplate().update("delete c from aweme_collection c left join aweme a on c.aweme_id = a.o_aweme_id where a.o_create_time > ?",time);
        JdbcTemplateHelper.getJdbcTemplate().update("delete c from aweme_collection c left join aweme a on c.aweme_id = a.o_aweme_id where a.o_aweme_id is null");
    }
}
